package ink.lovejinhu.leetcodeMatch;

import java.util.Arrays;

/**
 * @author jinhu
 * created 2021-08-17 20:15
 */
public class Solution_1968 {
    /**
     * i-1  i   i+1   i+2   i+3 i+4
     * [i]!=[i-1=+[i+1]
     * [i+1]!=[i]+[i+2]
     * [i+2]!=[i+1]+[i+3]
     *
     * @param nums
     * @return
     */
    public int[] rearrangeArray(int[] nums) {
        Arrays.sort(nums);
        int[] res = new int[nums.length];
        if (nums.length % 2 == 0) {
            int k = 0;
            for (int i = 0, j = nums.length / 2; k < nums.length; i++, j++) {
                res[k] = nums[i];
                res[k + 1] = nums[j];
                k += 2;
            }
        } else {
            int k = 0;
            for (int i = 0, j = nums.length / 2; k < nums.length - 1; i++, j++) {
                res[k] = nums[j];
                res[k + 1] = nums[i];
                k += 2;
            }
            res[nums.length - 1] = nums[nums.length - 1];
        }
        return res;

    }

    public static void main(String[] args) {
        Solution_1968 solution_1968 = new Solution_1968();
        int[] res = {0, 4, 1, 5, 3};//0  1  3 4 5
        int[] ints = solution_1968.rearrangeArray(res);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);

        }
    }
}
